/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sfsBeans;

import EL.Foods;
import EL.OrderDetails;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;

/**
 *
 * @author Tian
 */
@Stateful
public class ShoppingCartBLO implements ShoppingCartBLORemote
{

    private ArrayList<OrderDetails> foodCart;

    @PostConstruct
    public void init()
    {
        foodCart = new ArrayList<OrderDetails>();
    }

    public void addFood2Cart(OrderDetails orderDetail)
    {
        if(foodCart.contains(orderDetail))
        {
            
            OrderDetails od = foodCart.get(foodCart.indexOf(orderDetail));
            od.setQuantity(od.getQuantity() + orderDetail.getQuantity());
        }
        else
        {
            
            foodCart.add(orderDetail);
        }
    }

    public ArrayList<OrderDetails> viewFoodCart()
    {
        return foodCart;
    }

    public float totalFoodPrice()
    {
        float total= 0;
        for(OrderDetails od : foodCart)
        {
            total += od.getFoods().getPrice()  * od.getQuantity();
        }
        return total;
    }

    public void remove(OrderDetails orderDetail)
    {
        foodCart.remove(orderDetail);
    }

    public void update(OrderDetails orderDetail)
    {
        if(foodCart.contains(orderDetail))
        {

            OrderDetails od = foodCart.get(foodCart.indexOf(orderDetail));
            od.setQuantity(0);
            od.setQuantity(orderDetail.getQuantity());
        }
        else
        {

            foodCart.add(orderDetail);
        }
    }


}
